﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Logs.Core
{
    class LogRecordCollection : ILogRecordCollection
    {
        IXmlNodesCollection collection = null;

        public LogRecordCollection(IXmlNodesCollection collection)
        {
            this.collection = collection;
        }

        public ILogRecord this[int index]
        {
            get
            {
                IXmlNode subnode = collection[index];

                if (subnode == null)
                    return null;

                return new LogRecord(collection[index]);
            }
        }

        public ILogRecord this[string id]
        {
            get
            {
                if (string.IsNullOrEmpty(id))
                    throw new Exception("missing id");

                foreach (ILogRecord record in this)
                {
                    if (record.Id.Equals(id, StringComparison.OrdinalIgnoreCase))
                        return record;
                }

                return null;
            }
        }

        public int Count
        {
            get
            {
                return collection.Count;
            }
        }

        public void Add(ILogRecord record)
        {
            if (record == null)
                return;

            ILogRecord addrecord = this.Add(record.Id, record.RawDate, record.Level, record.Text, record.Ip);
            addrecord.Filepath = record.Filepath;

            foreach (ILogFrame frame in record.Frames)
            {
                addrecord.Frames.Add(frame);
            }

            return;
        }

        public ILogRecord Add(IXmlNode node)
        {
            if (node == null)
                return null;

            this.collection.Add(node);

            return new LogRecord(node);
        }

        public ILogRecord Add(string id, string date, LogLevel level, string text, string ip)
        {
            if (string.IsNullOrEmpty(id))
                return null;

            if (string.IsNullOrEmpty(date))
                return null;

            if (Contains(id))
                throw new Exception("duplicate id of record existed. id = " + id);

            IXmlNode addnode = collection.Add(StringHelper.default_element_log);
            addnode.Properties.Add(StringHelper.default_attribute_id, id.Trim());
            addnode.Properties.Add(StringHelper.default_attribute_time, date.Trim());
            addnode.Properties.Add(StringHelper.default_attribute_level, level.ToString());
            if (!string.IsNullOrEmpty(ip))
                addnode.Properties.Add(StringHelper.default_attribute_ip, ip.Trim());
            addnode.Text = string.IsNullOrEmpty(text) ? null : text.Trim();
            LogRecord addrecord = new LogRecord(addnode);
            return addrecord;
        }

        public bool Contains(string id)
        {
            return this[id] != null;
        }

        public void Sort()
        {
            List<ILogRecord> list = new List<ILogRecord>();

            foreach (ILogRecord rec in this)
            {
                list.Add(rec);
            }

            list.Sort();

            this.Clear();

            foreach (ILogRecord rec in list)
            {
                this.Add(rec);
            }
        }

        public void Clear()
        {
            collection.Clear();
        }


        #region IEnumerable<ILogRecord> 成员

        public IEnumerator<ILogRecord> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<ILogRecord> 成员

        public ILogRecord Current
        {
            get
            {
                return new LogRecord(collection.Current);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            collection.Dispose();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return new LogRecord(collection.Current);
            }
        }

        public bool MoveNext()
        {
            return collection.MoveNext();
        }

        public void Reset()
        {
            collection.Reset();
        }

        #endregion

    }
}
